﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICeFramework.DataManagement.Interfaces;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using ICeFramework.DataManagement.Collections;
using ICeFramework.DataAccess.Interfaces;
using ICeFramework.DataAccess.SqlConnections;
using ICeFramework.Common.DataLayer.Exceptions;

namespace ICeFramework.DataManagement.MSSql
{
    public class MSSqlServer : IServer
    {
        private Server _server;
        public Server Server
        {
            get { return _server; }
        }

        #region public MSSqlServer(ISqlConnection connection)
        /// <summary>
        /// Initializes a new instance of the <b>MSSqlServer</b> class.
        /// </summary>
        /// <param name="connection"></param>
        public MSSqlServer(ISqlConnection connection)
        {
            try
            {
                ServerConnection serverConnection = new ServerConnection((connection as MSSqlServerConnection).Connection);
                _server = new Server(serverConnection);                
                _name = _server.Name;
                _databases = new SqlDatabaseList(_server);
            }
            catch (Exception ex)
            {
                throw new DataAccessException("Error initializing server data !", ex);
            }
        }
        #endregion

        public void RefreshDatabases()
        {

            if (_server == null)
            {
                return;
            }
            _databases.Clear();
            foreach (Database db in _server.Databases)
            {
                if (!db.IsSystemObject)
                {
                    IDatabase mssqlDb = new MSSqlDatabase(this, db);
                    _databases.Add(mssqlDb);
                }
            }
        }

        #region public override string ToString()
        /// <summary>
        /// Returns a <see cref="string"/> that represents the current <see cref="ICeFramework.DataAccess.DataAccess.MSSql.MSSqlServer"/>.
        /// </summary>
        /// <returns>A <see cref="string"/> that represents the current <see cref="ICeFramework.DataAccess.DataAccess.MSSql.MSSqlServer"/>.</returns>
        public override string ToString()
        {
            return _name;
        }
        #endregion

        private string _name;
        #region public string Name
        /// <summary>
        /// Gets the Name of the MSSqlServer
        /// </summary>
        /// <value></value>
        public string Name
        {
            get { return _name; }
        }
        #endregion

        private SqlDatabaseList _databases;
        #region public SqlDatabaseList Databases
        /// <summary>
        /// Get/Sets the Databases of the MSSqlServer
        /// </summary>
        /// <value></value>
        public SqlDatabaseList Databases
        {
            get
            {
                return _databases;
            }
            set
            {
                _databases = value;
            }
        }
        #endregion
    }
}
